﻿// linkup url, id, Sound file together.
package com.cf.core.media{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import com.cf.core.events.LoadEvent;
	public class SoundFX extends EventDispatcher{
		private var G_onload:Boolean;
		private var G_fail:Boolean;
		private var G_channel:SoundChannel = new SoundChannel;
		private var G_playing:Boolean;
		private var G_pause:Boolean;
		private var G_position:Number;
		private var G_beforeMute:Boolean;
		private var G_id:*;
		public function set id(_v:*):void{G_id=_v}
		public function get id():*{return G_id}
		private var G_url:String;
		public function set url(_u:*):void{G_url=_u}
		public function get url():*{return G_url}
		private var G_sound:Sound;
		public function set sound(_f:*):void{G_sound=_f}
		public function get sound():*{return G_sound}
		private var G_loop:Boolean;
		public function set loop(_t:*):void{G_loop=_t}
		public function get loop():*{return G_loop}
		private var G_autoplay:Boolean;
		public function set autoplay(_t:*):void{G_autoplay=_t; if(G_onload) play();}
		public function get autoplay():*{return G_autoplay}
		private var G_mute:Boolean;
		public function set mute(_t:*):void{G_mute=_t; handleMute()}
		public function get mute():*{return G_mute}
		private var G_vol:Number;
		public function set volume(_n:Number):void{G_vol=_n; setVol()}
		public function get volume():Number{return G_vol}
		
		
		public function SoundFX():void{
			G_id=false;
			G_url="";
			G_sound=new Sound;
			G_onload=false;
			G_playing=false;
			G_loop=false;
			G_pause=false;
			G_autoplay=false;
			G_mute=false;
			G_position=0;
		}//fn
		
		public function load(_u:String):void{
			G_onload=false;
			G_fail=false;
			G_url=_u;
			G_sound=new Sound;
			G_sound.addEventListener(Event.COMPLETE, completeHandler);
			// G_sound.addEventListener(Event.ID3, id3Handler);
			G_sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			G_sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			G_sound.load(new URLRequest(_u));
			this.volume=1;
		}//fn
		
		public function play():void{
			if(G_onload && !G_fail && !G_playing && !mute){
				//trace("SoundFX ["+id+"]play : "+ G_position);
				G_channel=G_sound.play(G_position);
				this.setVol();
				if(!G_channel.hasEventListener(Event.SOUND_COMPLETE)){
					G_channel.addEventListener(Event.SOUND_COMPLETE, handleLoop);
				}//if
				G_pause=false;
				G_playing=true;
			}//if
		}//fn
		public function pause():void{
			G_position = G_channel.position;
			G_playing=false;
			G_pause=true;
			G_channel.stop();
		}//fn
		public function stop():void{
			G_position = 0;
			G_playing=false;
			G_channel.stop();
		}//fn
		
		private function handleMute():void{
			if(mute){
				G_beforeMute=G_playing;
				pause();
			}else{
				if(G_beforeMute){
					play();
				}//if
			}//if
		}//fn
		
		private function handleLoop(e:Event):void{
			//trace("SoundFX handle Loop");
			G_playing=false;
			if(loop){
				G_position = 0;
				play();
			}else{
				G_position = 0;
				stop();
			}//if
		}//fn
		
		private function completeHandler(event:Event):void {
			//trace("completeHandler: " + event);
			G_onload=true;
			if(autoplay) play();
			dispatchEvent(new LoadEvent(LoadEvent.ONLOAD, {id:id, url:url}) );
			G_sound.removeEventListener(Event.COMPLETE, completeHandler);
		}//fn
		private function id3Handler(event:Event):void {
			//trace("id3Handler: " + event);
		}//fn
		private function ioErrorHandler(event:Event):void {
			//trace("ioErrorHandler: " + event);
			G_onload=true;
			G_fail=true;
			dispatchEvent(new LoadEvent(LoadEvent.FAIL, {id:id, url:url}) );
		}//fn
		private function progressHandler(event:ProgressEvent):void {
			//trace("progressHandler: " + event);
			dispatchEvent(new LoadEvent(LoadEvent.ONPROGRESS, {id:id, url:url, bytesLoaded:event.bytesLoaded, bytesTotal:event.bytesTotal}) );
			G_sound.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}//fn
		
		public function RELEASE():void{
			try{
				this.stop();
				if(!G_channel.hasEventListener(Event.SOUND_COMPLETE)) G_channel.removeEventListener(Event.SOUND_COMPLETE, handleLoop);
				this.sound=new Sound();
				G_channel = new SoundChannel;
				G_sound.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			}catch(e){}
		}//fn
		private function setVol():void{
			var transform:SoundTransform = new SoundTransform(G_vol, 0);
			G_channel.soundTransform = transform;
		}//fn
	}//class
}//package